<?php
/* 展馆公告相关操作控制器 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class NoticeController extends HomeController
{
    //获取公告列表
    public function list(Request $request)
    {
        try {
            // 分页参数
            $page = $request->input('page', 1);
            $limit = $request->input('limit', 10);
            $search_title = $request->input('search_title');

            //要查询的字段
            $field = ['id', 'title', 'is_top', 'add_id', 'created_at'];

            $condition = [];
            if ($search_title) {
                $condition[] = ['title', 'like', '%' . $search_title . '%'];
            }

            // 查询数据表
            $data = DB::table('notice')->where($condition)->select($field)->orderBy('is_top', 'desc')->orderBy('created_at', 'desc')->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();
            // 统计总记录数
            $count = DB::table('notice')->count();

            //获取所有的添加人id
            $ids = array_column($data, 'add_id');
            //根据添加人id查询添加人姓名
            $add_names = DB::table('sys_user')->whereIn('id', $ids)->pluck('real_name', 'id');

            foreach ($data as $key => $value) {
                $data[$key]->add_name = $add_names[$value->add_id];
            }

            return response()->json([
                'code' => HTTP_OK,
                'message' => HTTP_OK_MESSAGE,
                'data' => $data,
                'count' => $count,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取公告列表失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '获取公告列表失败',
                'data' => null
            ]);
        }
    }

    //添加
    public function insert(Request $request)
    {
        try {
            //接受参数并组装成数组
            $data = [];
            $data['title'] = $request->input('title');
            $data['content'] = $request->input('content');
            $data['is_top'] = $request->input('is_top');
            $data['add_id'] = $this->user_id;

            //添加到数据表
            DB::table('notice')->insert($data);

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('添加公告失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '添加公告失败',
                'data' => null
            ]);
        }
    }

    //删除
    public function delete(Request $request)
    {
        try {
            //接受参数
            $id = $request->input('id');
            //删除数据
            DB::table('notice')->where('id', $id)->delete();

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('删除公告失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '删除公告失败',
                'data' => null
            ]);
        }
    }


    //修改
    public function update(Request $request)
    {
        try {
            //接受参数
            $id = $request->input('id');

            //接受参数并组装成数组
            $data = [];
            $data['title'] = $request->input('title');
            $data['content'] = $request->input('content');
            $data['is_top'] = $request->input('is_top');

            //修改数据到数据表
            DB::table('notice')->where('id', $id)->update($data);

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('修改公告失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '修改公告失败',
                'data' => null
            ]);
        }
    }

    //详情
    public function detail(Request $request)
    {
        try {
            //接受参数
            $id = $request->input('id');
            $field = ['title', 'content', 'is_top'];
            //根据id查询详情
            $data = DB::table('notice')->where('id', $id)->select($field)->first();

            return response()->json(['code' => HTTP_OK,'message' => HTTP_OK_MESSAGE,'data' => $data]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取公告详情失败: ' . $e->getMessage());
            return response()->json([
                'code' => HTTP_ERROR,
                'message' => '获取公告详情失败',
                'data' => null
            ]);
        }
    }

}
